package com.orders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.orders.pojo.User;

public class UserOperation implements Operation{
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	private User user ;
	@Override
	public boolean insert(Object obj) {
			try {
				if(obj instanceof User){
					user = (User)obj;
					conn = ConnectionFactory.getConnection();
					ps = conn.prepareStatement("insert into user values (?,?,?,?,?,?)");
					ps.setInt(1, user.getId());
					ps.setString(2, user.getRole());
					ps.setString(3, user.getUsername());
					ps.setString(4, user.getPassword());
					ps.setString(5, user.getNickname());
					ps.setBoolean(6, user.isVisible());
					return !ps.execute();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return false;
	}

	@Override
	public boolean update(Object obj) {
		try {
			if(obj instanceof User){
				user = (User)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update user set role = ?,username = ?,password = ?,nickname = ?,visible = ? where id =?");	
				ps.setString(1, user.getRole());
				ps.setString(2, user.getUsername());
				ps.setString(3, user.getPassword());
				ps.setString(4, user.getNickname());
				ps.setBoolean(5, user.isVisible());
				ps.setInt(6, user.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return false;
	}

	@Override
	public List select(Object obj) {
		List list = new ArrayList<>();
		try {
			if(obj instanceof User){
				user = (User)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from user where id = ? and visible = 1");
				ps.setInt(1, user.getId());
				rs = ps.executeQuery();
				while(rs.next()){
					user.setId(rs.getInt("id"));
					user.setUsername(rs.getString("username"));
					user.setPassword(rs.getString("password"));
					user.setNickname(rs.getString("nickname"));
					user.setRole(rs.getString("role"));
					list.add(user);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public User selectById(int id) {

		try {	
				user = new User();
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from user where id = ? and visible = 1");
				ps.setInt(1, id);
				rs = ps.executeQuery();
				rs.next();
					user.setId(rs.getInt("id"));
					user.setUsername(rs.getString("username"));
					user.setPassword(rs.getString("password"));
					user.setNickname(rs.getString("nickname"));
					user.setRole(rs.getString("role"));		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	
	public User selectByUsername(String username) {

		try {	
				user = new User();
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from user where username = ? and visible = 1");
				ps.setString(1, username);
				rs = ps.executeQuery();
				rs.next();
					user.setId(rs.getInt("id"));
					user.setUsername(rs.getString("username"));
					user.setPassword(rs.getString("password"));
					user.setNickname(rs.getString("nickname"));
					user.setRole(rs.getString("role"));		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	
	@Override
	public boolean delete(Object obj) {
		try {
			if(obj instanceof User){
				user = (User)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update user set visible = ? where id = ?");
				ps.setBoolean(1, false);
				ps.setInt(2, user.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return false;
	}

}
